﻿#include "weathertool.h"

WeatherTool::WeatherTool(QObject *parent) : QObject(parent)
{
    QString filename = QCoreApplication::applicationDirPath();
    QJsonParseError err;

    filename += "/citycode-2019-08-23.json";
    QFile file(filename);
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QByteArray json = file.readAll(

                );
    file.close();

    // 读取json数据
    QJsonDocument jsonDoc = QJsonDocument::fromJson(json, &err);
    // 获取城市列表数组
    QJsonArray citys = jsonDoc.array();
    for(int i = 0; i < citys.size(); i++) {
        QString code = citys.at(i).toObject().value("city_code").toString();
        QString cityName = citys.at(i).toObject().value("city_name").toString();
        // 省份的code是空的
        if(code > 0) {
            m_mapCityCode.insert(std::pair<QString, QString>(cityName, code));
        }
    }
}

